package com.onlyphoto.onlymall.utils;
/**
 *                                  有注释代码
 * 跳转控制类
 * 非线程安全
 */

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.onlyphoto.onlymall.mvp.view.activity.LoginActivity;
import com.onlyphoto.onlymall.mvp.view.activity.MainActivity;
import com.onlyphoto.onlymall.mvp.view.activity.OrderDetailsActivity;
import com.onlyphoto.onlymall.mvp.view.activity.WebViewActivity;

public class SchemeTool {

    public static String MyScheme = "schemeonly";
    public static final String TAG_JUMP_ORDER_DETAILS = "1";    //订单详情
    public static final String TAG_JUMP_LEND_HOME = "101";      //’首页’ tab1
    public static final String TAG_JUMP_LEND_REPAY = "102";     //’我的’ tab3
    public static final String TAG_JUMP_H5 = "103";             //’H5’


    public static String url = "";//跳转的URL

    public static String skip_code = "";
    private static String orderId = "";
    public static boolean isJump = false;

    /**************
     * 短信拉起的 有数据上报
     *
     * @param uri
     */

    private static Uri skipUri;

    public static void scheme(Uri uri) {
        if (uri == null || uri.getScheme() == null) {
            return;
        }
        skipUri = uri;
        LogUtil.log("skipUri", "skipUri" + skipUri.toString());
        if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) {
            SchemeTool.skip_code = "108";
            SchemeTool.url = uri.toString();
        } else if (MyScheme.equals(uri.getScheme())) {
            SchemeTool.skip_code = uri.getQueryParameter("skip_code");
            SchemeTool.url = uri.getQueryParameter("url");
        }
        isJump = true;
    }

    /*************
     * 非短信拉起 无上报
     *
     * @param uri
     */
    public static void schemeWithNoReport(Uri uri) {
        if (uri == null || uri.getScheme() == null) {
            return;
        }
        skipUri = uri;

        LogUtil.log("skipUri:", "skipUri" + skipUri.toString());
        if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) {
            SchemeTool.skip_code = "108";
            SchemeTool.url = uri.toString();
        } else if (MyScheme.equals(uri.getScheme())) {
            SchemeTool.skip_code = uri.getQueryParameter("skip_code");
            if (uri.getQueryParameter("order_id") != null) {
                SchemeTool.orderId = uri.getQueryParameter("order_id");
            }
            String url = uri.getQueryParameter("url");
            SchemeTool.url = url;
        }
        isJump = true;
    }

    /*********
     * 跳转web
     *
     * @param context
     */
    private static boolean jumpWebView(Context context) {
        if (!TAG_JUMP_H5.equals(skip_code)) {
            return false;
        }
        if (url == null || url.trim().length() == 0) {
            return false;
        }

        LogUtil.log("jumpWebView:", "url=" + url);
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
        url = null;
        return true;
    }

    /*************
     * 跳转到主页后处理
     */
    public static void jumpToMain(Uri uri, Context context) {
        if (StringUtil.isBlank(skip_code)) {
            schemeWithNoReport(uri);
        }

        isJump = true;
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
        if (!(context instanceof MainActivity)) {
            ((Activity) context).finish();
        }
    }

    /**********
     * 页面简单跳转
     *
     * @param uri
     */
    public static boolean jumpOnly(Uri uri, final Context context) {
        boolean flag = false;
        if (StringUtil.isBlank(skip_code)) {
            schemeWithNoReport(uri);
        }

        if (jumpWebView(context)) {
            flag = true;
        } else if (TAG_JUMP_LEND_HOME.equals(skip_code)) {
            flag = true;
            context.startActivity(new Intent(context, MainActivity.class).putExtra(MainActivity.PAGE_INDEX, Constants.PAGE_HOME));
        } else if (TAG_JUMP_LEND_REPAY.equals(skip_code)) {
            flag = true;
            context.startActivity(new Intent(context, MainActivity.class).putExtra(MainActivity.PAGE_INDEX, Constants.PAGE_ACCOUNT));
        } else if (TAG_JUMP_ORDER_DETAILS.equals(skip_code)) {
            flag = true;
            context.startActivity(new Intent(context, OrderDetailsActivity.class).putExtra("orderId", Integer.parseInt(orderId)));
        }

        LogUtil.log("jumpOnly", "skipUri" + skipUri.toString());
        if (flag) {
            skip_code = "";
            url = "";
            skipUri = null;
            isJump = false;
        }

        return flag;
    }

    /***************
     * 跳转
     *
     * @param uri
     */
    public static void jump(Uri uri, Context context) {
        if (uri == null || StringUtil.isBlank(uri.getScheme())) {
            return;
        }

        schemeWithNoReport(uri);

        if (TextUtils.isEmpty(skip_code)) {
            LogUtil.log("", "skipCode is empty");
            isJump = false;
            return;
        }
        if (!jumpOnly(uri, context)) {
            jumpToMain(uri, context);
        }
    }

    /************
     * 判断跳转的逻辑
     *
     * @param context
     */
    public static void isNeedJump(Context context) {
        if (isJump) {
            schemeWithNoReport(skipUri);

            if (TextUtils.isEmpty(skip_code)) {

                LogUtil.log("", "skipCode is empty");
                isJump = false;
                return;
            }

            if (context instanceof MainActivity) {
                if (!SchemeTool.jumpOnly(skipUri, context)) {

                    LogUtil.log("skipCode", "skipCode" + skip_code);

                    /*HomeIndexEntity.BannerListBean.Rotation bean = new HomeIndexEntity.BannerListBean.Rotation();
                    bean.setSkipCode(skip_code);
                    EventBus.getDefault().post(bean);*/

                    skip_code = "";
                    url = "";
                    skipUri = null;
                    isJump = false;
                }
            } else {
                SchemeTool.jump(skipUri, context);
            }
        }
    }

    public static void goLogin(Context context) {
        String phone = SharePreferenceUtil.getInstance(context).getData(Constants.SHARE_TAG_LOGIN_PHONE);
        if (!StringUtil.isBlank(phone)) {
            Intent intent = LoginActivity.Companion.makeIntent(context, phone, 0);
            context.startActivity(intent);
        } else {
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
        }
    }
}
